package com.af.monitorModule.generator.service.function;

import com.af.monitorModule.generator.entity.Company;
import com.af.monitorModule.generator.entity.DayMonitorNum;
import com.af.monitorModule.generator.entity.Monitor;
import com.af.monitorModule.generator.entity.Server;
import com.af.monitorModule.generator.mapper.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FunctionService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private ServerMapper serverMapper;

    @Autowired
    private MonitorMapper monitorMapper;

    @Autowired
    private UtilMapper utilMapper;

    @Autowired
    private DayMonitorNumMapper dayMonitorNumMapper;

    @Autowired
    private ValueOperations<String,Object> valueOperations;

    /**
     * 返回首页的一些统计类数据
     * @return
     */
    public JSONObject getWelcome(){
        JSONObject res = new JSONObject();

        //welcome数据统计
        JSONObject statistics = new JSONObject();
        statistics.put("company",companyMapper.selectCount(new QueryWrapper<Company>().eq("state",1)));
        statistics.put("monitor",monitorMapper.selectCount(new QueryWrapper<Monitor>().eq("state",1)));
        statistics.put("server",serverMapper.selectCount(new QueryWrapper<Server>().eq("state",1)));
        statistics.put("toDaySendMSG",valueOperations.get("toDaySendMSG"));
        statistics.put("toDayServerPush",valueOperations.get("toDayServerPush"));
        statistics.put("toDayMeaningless",valueOperations.get("toDayMeaningless"));
        statistics.put("toDayServiceRestart",valueOperations.get("toDayServiceRestart"));
        statistics.put("toDayKillDataBase",valueOperations.get("toDayKillDataBase"));

        //处理返回结构
        res.put("code",200);
        res.put("statistics",statistics);
        return res;
    }

    /**
     * 返回首页的公司主要信息
     * @return
     */
    public JSONObject getWelcomeCompany(){
        JSONObject res = new JSONObject();
        List<Company> state = companyMapper.selectList(new QueryWrapper<Company>().eq("state", 1));
        res.put("data",state);
        res.put("code",0);
        res.put("msg","客户列表");
        return res;
    }

    /**
     * 返回首页的监控服务器信息
     * @return
     */
    public JSONObject getWelcomeServer(){
        JSONObject res= new JSONObject();
        List<Map<String, Object>> welcomeServer = utilMapper.getWelcomeServer();
        res.put("data",welcomeServer);
        res.put("code",0);
        res.put("msg","服务器列表");
        return res;
    }

    /**
     * 获取首页近日的折线图数据
     * @param day
     * @return
     */
    //获取三类目的折线图数据
    public JSONObject getWelcomeCountLine(String day){
        int old = day == null? 7: Integer.parseInt(day);
        JSONObject res = new JSONObject();
        QueryWrapper<DayMonitorNum> wrapper = new QueryWrapper<>();
        wrapper.ge("data_date", LocalDate.now().plusDays(old * -1).toString());
        wrapper.orderByAsc("data_date");
        List<DayMonitorNum> dayMonitorNums = dayMonitorNumMapper.selectList(wrapper);

        res.put("code",200);
        res.put("data",dayMonitorNums);
        return res;
    }
}
